package com.galaxy.order.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.galaxy.common.mybatis.core.page.PageQuery;
import com.galaxy.common.mybatis.core.page.TableDataInfo;
import com.galaxy.order.domain.OrderRefund;
import com.galaxy.order.domain.bo.OrderRefundBo;
import com.galaxy.order.domain.vo.OrderRefundVo;
import com.galaxy.order.domain.vo.OrderVo;
import com.galaxy.order.mapper.OrderMapper;
import com.galaxy.order.mapper.OrderRefundMapper;
import com.galaxy.order.service.OrderRefundService;
import io.github.linpeilie.Converter;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 订单退款信息Service业务层处理
 */
@RequiredArgsConstructor
@Service
public class OrderRefundServiceImpl extends ServiceImpl<OrderRefundMapper, OrderRefund> implements OrderRefundService {

    private final OrderRefundMapper orderRefundMapper;
    private final Converter converter;

    private final OrderMapper orderMapper;

    @DS("slave")
    @Override
    public TableDataInfo<OrderRefundVo> queryPageList(OrderRefundBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<OrderRefund> lqw = buildQueryWrapper(bo);
        var page = orderRefundMapper.selectPage(pageQuery.build(), lqw);
        List<OrderRefundVo> records = page.getRecords().stream()
            .map(orderRefund -> converter.convert(orderRefund, OrderRefundVo.class))
            .collect(Collectors.toList());
        for(OrderRefundVo orderRefundVo : records){
            orderRefundVo.setOrder(converter.convert(orderMapper.selectById(orderRefundVo.getOrderId()), OrderVo.class));
        }

        return new TableDataInfo<>(records, page.getTotal());
    }

    @DS("slave")
    @Override
    public List<OrderRefundVo> queryList(OrderRefundBo bo) {
        LambdaQueryWrapper<OrderRefund> lqw = buildQueryWrapper(bo);
        List<OrderRefundVo> collect = orderRefundMapper.selectList(lqw).stream()
            .map(orderRefund -> converter.convert(orderRefund, OrderRefundVo.class))
            .collect(Collectors.toList());
        for(OrderRefundVo orderRefundVo : collect){
            orderRefundVo.setOrder(converter.convert(orderMapper.selectById(orderRefundVo.getOrderId()), OrderVo.class));
        }
        return collect;
    }

    @DS("slave")
    @Override
    public OrderRefund queryById(Long id) {
        OrderRefund orderRefund = orderRefundMapper.selectById(id);
        orderRefund.setOrder(converter.convert(orderMapper.selectById(orderRefund.getOrderId()), OrderVo.class));
        return orderRefund;
    }

    @Override
    public Boolean insertByBo(OrderRefundBo bo) {
        OrderRefund add = converter.convert(bo, OrderRefund.class);
        return orderRefundMapper.insert(add) > 0;
    }

    @Override
    public Boolean updateByBo(OrderRefundBo bo) {
        OrderRefund update = converter.convert(bo, OrderRefund.class);
        return orderRefundMapper.updateById(update) > 0;
    }

    @Override
    public Boolean deleteWithValidByIds(List<Long> ids, Boolean isValid) {
        if (isValid) {
            // TODO: Add validation logic here
        }
        return orderRefundMapper.deleteBatchIds(ids) > 0;
    }

    private LambdaQueryWrapper<OrderRefund> buildQueryWrapper(OrderRefundBo bo) {
        LambdaQueryWrapper<OrderRefund> lqw = Wrappers.lambdaQuery();
        lqw.like(bo.getOrderNo() != null, OrderRefund::getOrderNo, bo.getOrderNo());
        lqw.eq(bo.getOrderId() != null, OrderRefund::getOrderId, bo.getOrderId());
        lqw.eq(bo.getUserId() != null, OrderRefund::getUserId, bo.getUserId());
        lqw.eq(bo.getStatus() != null, OrderRefund::getStatus, bo.getStatus());
        return lqw;
    }
}
